/* 
 * File:   QCamera.cpp
 * Author: matt
 * 
 * Created on 16 May 2010, 19:07
 */

#include "QCamera.h"


using namespace cv;
QCamera::QCamera() {
    working = false;
    working = capture.open(0);

}

QCamera::QCamera(int camera_number)
{
    working = false;
    working = capture.open(camera_number);
}

QCamera::~QCamera() {
    capture.release();
}

void QCamera::getImage()
{
    cerr<<"Capturing new frame"<<endl;
    capture>>cv_frame;

    if(working == false)
    {
        frame = QImage(640, 480, QImage::Format_RGB32);
        frame.fill(qRgb(0,255,0));
        return;
    }

    frame = QImage(cv_frame.cols,cv_frame.rows, QImage::Format_RGB32);

    for(int y= 0 ; y< cv_frame.rows; y++)
    {
        for(int x=0; x<cv_frame.cols; x++)
        {
            Vec3b cv_pixel = cv_frame.at<Vec3b>(y,x);
            uchar red =cv_pixel.val[2];
            uchar green =cv_pixel.val[1];
            uchar blue = cv_pixel.val[0];
            int pixel = qRgb(red,green,blue);
            frame.setPixel(x,y,pixel);
        }
    }
    //cv_frame.at()

    emit(newImage(frame));
}